package com.kim.file;

import java.io.File;

import android.os.Environment;
import android.os.StatFs;

public class SDCardUtils {

	/**
	 * SDCard根路径
	 */
	public static File fileDir = null;

	/**
	 * SDCard目录信息对象
	 */
	public static StatFs statFs = null;

	private static final int SDCARD_LIMITED_SIZE = 1024 * 1024 * 8; // SDCard至少保证8M空间

	public static final boolean sdcardAvailable() {
		return (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState()));
	}

	/**
	 * 获取SDCard根目录
	 * 
	 * @return
	 */
	public static final File getSDCardBaseDir() {
		if (!sdcardAvailable()) {
			return null;
		} else {
			if (fileDir == null) {
				fileDir = Environment.getExternalStorageDirectory();
			}
		}
		return fileDir;
	}

	/**
	 * 获取SDCard目录信息对象
	 * 
	 * @return
	 */
	public static final StatFs getSDCardStatFs() {
		if (!sdcardAvailable()) {
			return null;
		} else {
			if (statFs == null) {
				if (getSDCardBaseDir() != null)
					statFs = new StatFs(fileDir.getPath());
			}
		}
		return statFs;
	}

	/**
	 * 判断SDCard是否能写
	 * 
	 * @return
	 */
	public static final boolean sdcardCanWrite() {

		if (!sdcardAvailable()) { // SDCard未挂载
			return false;
		}
		return statFs.getAvailableBlocks() > SDCARD_LIMITED_SIZE;
	}

}
